// 展示
import { navigation } from '../../utils/commons'

Page({
    data: {
        list: [],
        // levelName:(function(){
        //   var m = new Map()
        //   m.set(-1,"优秀项目")
        //   return m;
        // }),
        selectItems: [{
            name: "项目名称",
            checked: false
        }, {
            name: "院系",
            checked: false
        }, {
            name: "负责人",
            checked: false
        }
        ],
        selectedName: "项目名称",
        searchValue: "",
        sessionID: "",

        // 搜索 request 参数
        parameters: {
            pageParam: {
                pageSize: 10000,
                pageNum: 1,
                orderBy: "id asc"
            },
            type: 2,
            sortType:0,
            order:0,
            name: "",
            department: "",
            captain: ""
        }
    },

    // 载入页面
    onLoad() {
        // 默认按照项目名称搜索
        this.setData({
            'selectItems[0].checked': true,
            sessionID: wx.getStorageSync('sessionID')
        });

        // 获取后端数据，显示项目列表
        wx.request({
            url: 'https://www.hschuangsai.com/displayProject/list',
            method: 'POST',
            data: {
                pageParam: {
                    pageSize: 10000,
                    pageNum: 1,
                    orderBy: "id asc"
                },
                type: "ALL"
            },
            success: (res) => {
                console.log("display list",res.data.data.list)
                this.setData({
                    list: res.data.data.list
                })
            }
        });

        // 检测登录态
        wx.request({
            url: 'https://www.hschuangsai.com/user/check',
            method: 'GET',
            header: {
                'session': this.data.sessionID
            },
            success: (res) => {
                if (res.data.data === "登录失效") {
                    // 重新登录
                    wx.login({
                        success: (res) => {
                            wx.request({
                                url: 'https://www.hschuangsai.com/user/login/' + res.code,
                                method: 'GET',
                                success: (res) => {
                                    let sessionID = res.data.data.sessionId;
                                    wx.setStorageSync('sessionID', sessionID); // 缓存sessionID
                                    this.setData({
                                        sessionID: sessionID
                                    });
                                }
                            })
                        }
                    })
                }
            }
        });
    },

    // 改变搜索方式
    radioChange(e) {
        let index = e.detail.value;
        this.setData({
            selectedName: this.data.selectItems[index].name
        })
    },

    // 显示选择框
    showModal(e) {
        this.setData({
            modalName: e.currentTarget.dataset.target
        })
    },
    // 隐藏选择框
    hideModal(e) {
        this.setData({
            modalName: null,
        });
    },

    // input双向绑定
    inputChange(e) {
        let currentValue = e.detail.value;
        this.setData({
            searchValue: currentValue
        });
    },

    // 搜索
    search() {
        if (this.data.selectedName === "项目名称") {
            this.setData({
                "parameters.department": '',
                "parameters.name": this.data.searchValue,
                "parameters.captain": ''
            });
        }
        else if (this.data.selectedName === "院系") {
            this.setData({
                "parameters.department": this.data.searchValue,
                "parameters.name": '',
                "parameters.captain": ''
            });
        }
        else {
            this.setData({
                "parameters.department": '',
                "parameters.name": '',
                "parameters.captain": this.data.searchValue
            });
        }
        console.log(this.data.parameters)

        wx.request({
            url: 'https://www.hschuangsai.com/search/condition',
            method: 'POST',
            data: this.data.parameters,
            header: {
                'content-type': 'application/json'
            },
            success: (res) => {
                console.log("search list",res)
                this.setData({
                    list: res.data.data.list
                })
            }
        });
    },

    // 跳转至详情页
    goDetail(e) {
        let id = e.currentTarget.dataset.id;
        navigation('/pages/display/display-info/display-info?id=' + id);
    }
})
